#include "board.h"


uint8_t usart1_tx_buffer[] = "usart transfer by interrupt: usart3 -> usart2 using interrupt";
uint8_t usart1_rx_buffer[USART1_TX_BUFFER_SIZE];
volatile uint8_t usart1_tx_counter = 0x00;
volatile uint8_t usart1_rx_counter = 0x00;
uint8_t usart1_tx_buffer_size = USART1_TX_BUFFER_SIZE;
uint8_t usart1_rx_buffer_size = USART1_RX_BUFFER_SIZE;


PUTCHAR_PROTOTYPE
{
  while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET);
  usart_data_transmit(PRINT_UART, ch);
  return ch;
}

void uart_init(uint32_t baudrate)
{
	gpio_init_type gpio_init_struct;

	/* enable the uart and gpio clock */
	crm_periph_clock_enable(PRINT_UART_CRM_CLK, TRUE);
	crm_periph_clock_enable(PRINT_UART_TX_GPIO_CRM_CLK, TRUE);

	gpio_default_para_init(&gpio_init_struct);

	/* configure the uart tx pin */
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
	gpio_init_struct.gpio_pins = PRINT_UART_TX_PIN;
	gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	gpio_init(PRINT_UART_TX_GPIO, &gpio_init_struct);

	// configure uart rx pin
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
	gpio_init_struct.gpio_pins = PRINT_UART_RX_PIN;
	gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	gpio_init(PRINT_UART_TX_GPIO, &gpio_init_struct);

	// configure UART interrupt
	nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
	nvic_irq_enable(USART1_IRQn, 0, 0);
	/* configure uart param */
	usart_init(PRINT_UART, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
	usart_transmitter_enable(PRINT_UART, TRUE);
	usart_receiver_enable(PRINT_UART, TRUE);

	// interrupt enable
  	usart_interrupt_enable(PRINT_UART, USART_RDBF_INT, TRUE);
	usart_enable(PRINT_UART, TRUE);

	
}


